﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Applica.Models;
using System.Configuration;
using System.Net.Mail;

namespace Applica.Controllers
{
    public class HomeController : Controller
    {
         private homewizard_test2databaseEntities db = new homewizard_test2databaseEntities();
        [HttpPost]
        public ActionResult Login(SponsorInputs345xModel model, string returnUrl)
        {

            var checklogin = from e in db.SponsorInputs345x
                             where e.Spon_Username == model.Spon_Username
                                 && e.Spon_PASSWORD  == model.Spon_PASSWORD 
                             select e;

            if (checklogin.ToList().Count == 1)
            {

                String sessionSecurity=String.Empty;
                Random rn=new Random();
	            sessionSecurity = rn.Next(1, 40000000).ToString();
                Session["sessionSecurity"] = sessionSecurity;

                
                Session["Spon_Username"] = model.Spon_Username;


                var getsponser = (from e in db.SponsorInputs345x
                                 where e.Spon_Username == model.Spon_Username
                                     && e.Spon_PASSWORD == model.Spon_PASSWORD
                                 select e.sponsor).SingleOrDefault();
                Session["sponsor"] = getsponser;
                SendMail("niteshguleria@nascenture.com", getsponser + " has logged in to their Sponsor Dashboard", "", "Home-Wizard <admin@home-wizard.com>", "Sponsor Login");
                //FormsAuthentication.SetAuthCookie(model.Spon_PASSWORD, model.RememberMe);
                //if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                    //&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                //{
                   // return Redirect(returnUrl);
                //}
                //else
                //{
                    //String senderSP = "http://www.home-wizard.com/sponsor-dashboard.aspx?loginrequest=yes&loginname=" + model.Spon_Username.ToString() + "&sponsor=" + Session["sponsor"].ToString() + "&sessionSecurity=" + sessionSecurity;
                   
                    //return Redirect(senderSP);
               // }
                if (model.Spon_cookiecheck)
                {
                    //FormsAuthentication.RedirectFromLoginPage(model.Spon_Username, model.RememberMe);
                
                }
               return RedirectToAction("Index", "Iphone");

            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
                return View("Clientlogin");
            }

         
        }
        public ActionResult Forget(SponsorInputs345xModel model, string returnUrl)
        {
            var CheckPassword = from e in db.SponsorInputs345x
                                where e.Spon_E_Mail == model.iemail_address
                                select e;
          
             if (CheckPassword.ToList().Count == 1)
             {
                 string Password=string.Empty;
                 var PasswordChecked = (from e in db.SponsorInputs345x
                                        where e.Spon_E_Mail == model.iemail_address
                             select e).SingleOrDefault();
					Password =PasswordChecked.Spon_PASSWORD;
                    string msg = "Per your request, here is your login information for your client account with Applica Solutions: <br><br>client email address:&nbsp;&nbsp;" + model.iemail_address + "<br><br>client password:&nbsp;&nbsp;" + Password + "<br><br><br>You can login to your Home Wizard Sponsor Dashboard at:  http://www.applica-solutions.com/client-login.aspx" + "<br><br>And if you have any questions, you can contact us at support@applica-solutions.com or 978-443-7373.";
                    SendMail(model.iemail_address, msg, Password, "niteshguleria@nascenture.com", "Applica Forgotten Password (Home Wizard Sponsor Dashboard)");
                 ModelState.AddModelError("", "Please check you email account.");
                 return View("Clientforgotpassword");

             }
             else
             {
                 ModelState.AddModelError("", "The email address is incorrect.");
                 return View("Clientforgotpassword");
             }
        }
        private void SendMail(String Users, string message,string password,string from, string Title)
        {
            String msg = message;
            
            String title = Title;
           
            String toAccount = Users;
            //String toAccount = "niteshguleria@nascenture.com";
            String MyHost = ConfigurationManager.AppSettings["SMTPHost"].ToString();
            String MySMTPPort = ConfigurationManager.AppSettings["SMTPPort"].ToString();
            String MySMTPSSL = ConfigurationManager.AppSettings["SMTPSSL"].ToString();
            String fromAddr = from;   //Email Address of Sender
            MailMessage smtpMssg = new MailMessage();
            smtpMssg.From = new MailAddress(fromAddr);
            if (toAccount.Contains(","))
            {
                string[] tos = toAccount.Split(',');
                for (int i = 0; i < tos.Length; i++)
                {
                    if (tos[i] == "," || tos[i] == "")
                    {
                    }
                    else
                    {
                        smtpMssg.To.Add(new MailAddress(tos[i].Replace(",", "")));
                    }
                }
            }
            else
            {
                smtpMssg.To.Add(new MailAddress(toAccount));
            }
            smtpMssg.Subject = title;
            smtpMssg.Body = msg;
            smtpMssg.IsBodyHtml = true;
            System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(MyHost, Convert.ToInt32(MySMTPPort));
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            if (MySMTPSSL == "true")
            {
                smtpClient.EnableSsl = true;
            }

            try
            {
                smtpClient.Send(smtpMssg);
            }
            catch (Exception ex)
            {
                throw new Exception("Email not sent | Reason: [" + ex.Message + "]");
            }
        }
       
        public ActionResult Index()
        {
             return View();
        }
        public ActionResult Clientforgotpassword()
        {
            return View();
        }
        public ActionResult Clientlogin()
        {
            return View();
        }
        public ActionResult Contact()
        {
            return View();
        }
        public ActionResult Faq()
        {
            return View();
        }
        public ActionResult Homeprofessionals()
        {
            return View();
        }
        public ActionResult Petcareindustry()
        {
            return View();
        }
        public ActionResult Pricing()
        {
            return View();
        }
        public ActionResult Retailsolutions()
        {
            return View();
        }
        public ActionResult Services()
        {
            return View();
        }
        public ActionResult Signup()
        {
            return View();
        }
    }
}
